package com.stn.zjx.givingtreasure.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.allenliu.badgeview.BadgeFactory;
import com.allenliu.badgeview.BadgeView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.msg.MsgListItemActivity;
import com.stn.zjx.givingtreasure.domain.MsgTypeListDomain;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.RxActivityTool;

import java.util.ArrayList;
import java.util.List;

public class MsgTypeListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<MsgTypeListDomain.DataBeanXX.DataBeanX> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Context context;
    public int[] imgs=new int[]{R.mipmap.msg_office,R.mipmap.msg_notice,R.mipmap.msg_account};
   public MsgTypeListAdapter(Context context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_msg_type_list, parent, false));
       return vh;
   }
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
                MsgTypeListDomain.DataBeanXX.DataBeanX  dataBeanX= dataBeanList.get(i);
       BadgeView badgeView= BadgeFactory.create(context)
               .setTextColor(context.getResources().getColor(R.color.white))
               .setWidthAndHeight(14,14)
               .setBadgeBackground(context.getResources().getColor(R.color.red))
               .setTextSize(8)
               .setBadgeGravity(Gravity.RIGHT|Gravity.TOP)
               .setBadgeCount("0")
               .setShape(BadgeView.SHAPE_CIRCLE)
               .bind(ih.round_iv);
       if(dataBeanX.getCount()!=0){
           if(dataBeanX.getCount()>=99){
               badgeView.setBadgeCount("99+");
           }else{
               badgeView.setBadgeCount(""+dataBeanX.getCount());
           }
       }else{
           badgeView.setVisibility(View.GONE);
       }
                switch (i){
                    case 0:
                        Glide.with(context)
                                .asBitmap()
                                .load(imgs[0])
                                .into(new SimpleTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                     ih.round_iv.setImageBitmap(resource);
                                    }
                                });

                        break;
                    case 1:
                        Glide.with(context)
                                .asBitmap()
                                .load(imgs[1])
                                .into(new SimpleTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                        ih.round_iv.setImageBitmap(resource);
                                    }
                                });
                        break;
                    case 2:
                        Glide.with(context)
                                .asBitmap()
                                .load(imgs[2])
                                .into(new SimpleTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                        ih.round_iv.setImageBitmap(resource);
                                    }
                                });
                        break;

                }
                ih.msg_title.setText(dataBeanX.getTitle());
                if(dataBeanX.getData()!=null&&dataBeanX.getData().size()!=0){
                    ih.msg_content.setText(dataBeanX.getData().get(0).getMsgTitle());
                    ih.time.setText(ToolsUtils.getDateBeforeAfter(dataBeanX.getData().get(0).getMsgCreatedate()));
                }else{
                    ih.msg_content.setText(context.getString(R.string.no_data_msg));
                    ih.time.setText("");
                }


       ih.rootView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Bundle bundle=new Bundle();
               bundle.putString("msgTitle",dataBeanX.getTitle());
               bundle.putInt("msgType",dataBeanX.getMsgType());
               RxActivityTool.skipActivity(context, MsgListItemActivity.class,bundle);
           }
       });

   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<MsgTypeListDomain.DataBeanXX.DataBeanX> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<MsgTypeListDomain.DataBeanXX.DataBeanX> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
             public ImageView round_iv;
             public TextView msg_title;
             public  TextView msg_content;
             public  TextView time;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
           round_iv=itemView.findViewById(R.id.round_iv);
           msg_title=itemView.findViewById(R.id.msg_title);
           msg_content=itemView.findViewById(R.id.msg_content);
            time=itemView.findViewById(R.id.time);
        }
   }

}